package com.qst.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qst.order.domain.ReturnApply;
import com.qst.order.domain.VO.RefundDetailVO;
import com.qst.order.service.ReturnApplyService;
import com.qst.order.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 退货退款管理Controller
 * 处理退货相关的HTTP请求
 */
@RestController
@RequestMapping("/api/merchant/refunds")
public class RefundController {

    @Autowired
    private ReturnApplyService returnApplyService;

    /**
     * 分页查询退货申请列表
     */
    @GetMapping
    public Result<IPage<ReturnApply>> getRefundList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        IPage<ReturnApply> refundPage = returnApplyService.getRefundPage(
                page, size, keyword, status, startDate, endDate
        );
        return Result.success(refundPage);
    }

    /**
     * 根据ID查询退货详情
     */
    @GetMapping("/{returnId}")
    public Result<RefundDetailVO> getRefundDetail(@PathVariable Integer returnId) {
        RefundDetailVO detail = returnApplyService.getRefundDetail(returnId);
        if (detail == null) {
            return Result.error("退货申请不存在");
        }
        return Result.success(detail);
    }

    /**
     * 同意退货申请
     */
    @PostMapping("/{returnId}/agree")
    public Result<ReturnApply> agreeRefund(@PathVariable Integer returnId) {
        try {
            ReturnApply result = returnApplyService.agreeRefund(returnId);
            return Result.success("已同意退货申请",result);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 拒绝退货申请
     */
    @PostMapping("/{returnId}/reject")
    public Result<ReturnApply> rejectRefund(
            @PathVariable Integer returnId,
            @RequestBody RejectReasonDTO rejectReason
    ) {
        try {
            if (rejectReason.getReason() == null || rejectReason.getReason().trim().isEmpty()) {
                return Result.error("请输入拒绝理由");
            }
            ReturnApply result = returnApplyService.rejectRefund(returnId, rejectReason.getReason());
            return Result.success( "已拒绝退货申请",result);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 导出退货申请列表
     */
//    @GetMapping("/export")
//    public void exportRefundList(
//            @RequestParam(required = false) String keyword,
//            @RequestParam(required = false) Integer status,
//            @RequestParam(required = false) String startDate,
//            @RequestParam(required = false) String endDate,
//            HttpServletResponse response
//    ) {
//        returnApplyService.exportRefundList(keyword, status, startDate, endDate, response);
//    }

    /**
     * 拒绝理由DTO
     */
    static class RejectReasonDTO {
        private String reason;

        public String getReason() {
            return reason;
        }

        public void setReason(String reason) {
            this.reason = reason;
        }
    }
}
